#region Microsoft Public License
/*
 * (c) Copyright Chenjun Wu. http://utablesdk.codeplex.com
 * 
 * This source is subject to the Microsoft Public License (Ms-PL).
 * Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
 * All other rights reserved.
 */
#endregion

﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Windows.Markup;
using UTable.Input;
using UTable.Input.MultiTouch;

namespace UTable.Objects.Handlers
{
	[ContentProperty("Handlers")]
	public class MultiTouchProcessor
	{
		public MultiTouchProcessor()
		{
			// Default setting
			IsEnabled = true;
			collectionChangedHandler = new NotifyCollectionChangedEventHandler(handlers_CollectionChanged);
			handlers = new ObservableCollection<MultiTouchEventHandlerClass>();
			handlers.CollectionChanged += collectionChangedHandler;
		}

		#region Private Fields

		protected event MultiTouchEventHandler MultiTouch;

		private Dictionary<MultiTouchEventHandlerClass, MultiTouchEventHandler> handlersMap = new Dictionary<MultiTouchEventHandlerClass, MultiTouchEventHandler>();

		private ObservableCollection<MultiTouchEventHandlerClass> handlers;

		private NotifyCollectionChangedEventHandler collectionChangedHandler;
		
		#endregion


		#region Private Methods

		void handlers_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
		{
			switch (e.Action)
			{
				case NotifyCollectionChangedAction.Add:
                    AddHandler(e.NewItems[0] as MultiTouchEventHandlerClass);
					break;
				case NotifyCollectionChangedAction.Remove:
                    RemoveHandler(e.OldItems[0] as MultiTouchEventHandlerClass);
					break;
				case NotifyCollectionChangedAction.Move:
                    break;
				case NotifyCollectionChangedAction.Replace:
                    RemoveHandler(e.OldItems[0] as MultiTouchEventHandlerClass);
					AddHandler(e.NewItems[0] as MultiTouchEventHandlerClass);
					break;
				case NotifyCollectionChangedAction.Reset:
                    foreach (MultiTouchEventHandlerClass hander in handlersMap.Keys)
					{
						MultiTouch -= handlersMap[hander];
					}
					handlersMap.Clear();
					break;
			}
		}

		private void AddHandler(MultiTouchEventHandlerClass handlerClass)
		{
            //Console.WriteLine(handlerClass.GetType());
            if (handlersMap.ContainsKey(handlerClass))
				return;

			MultiTouchEventHandler handler = new MultiTouchEventHandler(handlerClass.OnMultiTouch);
            //Console.WriteLine("after: {0}", handlerClass.GetType());
			handlersMap.Add(handlerClass, handler);
			MultiTouch += handler;
		}

		private void RemoveHandler(MultiTouchEventHandlerClass handlerClass)
		{
			if (handlersMap.ContainsKey(handlerClass))
			{
				MultiTouch -= handlersMap[handlerClass];
				handlersMap.Remove(handlerClass);
			}
		}

		#endregion


		#region Methods

		public void RaiseMultiTouchEvent(MultiTouchEventArgs args)
		{
			if (MultiTouch != null)
			{
                MultiTouch(args);
			}
		}

		#endregion


		#region Properties

		public Boolean IsEnabled { get; set; }

		public ObservableCollection<MultiTouchEventHandlerClass> Handlers
		{
			get
			{
				return handlers;
			}
		}

		#endregion
	}
}
